package com.ad.nbi.alarmirpnotifications;

import java.util.Properties;  

import org.omg.CORBA.IntHolder;  
import org.omg.CORBA.ORB;  
import org.omg.CORBA.UserException;  
import org.omg.CosNotification.EventType;  
import org.omg.CosNotifyChannelAdmin.ClientType;  
import org.omg.CosNotifyChannelAdmin.ConsumerAdmin;  
import org.omg.CosNotifyChannelAdmin.EventChannel;  
import org.omg.CosNotifyChannelAdmin.EventChannelHelper;  
import org.omg.CosNotifyChannelAdmin.InterFilterGroupOperator;  
import org.omg.CosNotifyChannelAdmin.ObtainInfoMode;  
import org.omg.CosNotifyChannelAdmin.StructuredProxyPushSupplierHelper;  
import org.omg.CosNaming.NamingContextExt;  
import org.omg.CosNaming.NamingContextExtHelper;  
import org.omg.CosNotifyChannelAdmin.StructuredProxyPushSupplier;  
import org.omg.CosNotifyComm.StructuredPushConsumerHelper;  
import org.omg.CosNotifyFilter.ConstraintExp;  
import org.omg.CosNotifyFilter.Filter;  
import org.omg.CosNotifyFilter.FilterFactory;  
import org.omg.PortableServer.POA;  
import org.omg.PortableServer.POAHelper;  

import com.ad.nbi.epirpsystem.server.Server;
import com.ad.nbi.util.Utils;
  
public class Client{  
  
    public static void main(String[] args) {  
        try {  
            Properties props = new Properties();  
        	props.setProperty("org.omg.CORBA.ORBClass", "org.jacorb.orb.ORB");
    		props.setProperty("org.omg.CORBA.ORBSingletonClass", "org.jacorb.orb.ORBSingleton");
//    		props.setProperty("ORBInitRef.NameService", "corbaloc::192.168.0.128:30000/NameService"); 
    		props.setProperty("ORBInitRef.NotificationService", "corbaloc::192.168.0.57:30001/NotificationService");
              
    		String fileName = Server.IOR_BASE_PATH+Server.NTFY_SERVER_IOR;
    		String ior = Utils.getIorString(fileName);
    		System.out.println("ior:"+ior);
    		
            ORB orb = ORB.init(new String [] {ior}, props);  
            POA rootPoa = POAHelper.narrow(  
                    orb.resolve_initial_references("RootPOA"));  
            rootPoa.the_POAManager().activate();  
              
//            NamingContextExt ncExt = NamingContextExtHelper.narrow(  
//                    orb.resolve_initial_references("NameService"));  
//              
            EventChannel eventChannel = EventChannelHelper.narrow(  
            		orb.string_to_object(ior));  
//            EventChannel eventChannel = EventChannelHelper.narrow(  
//                    ncExt.resolve_str("NewsBC.NotifChannel"));  
            
    		
              
            createConsumerAllEvents(eventChannel, rootPoa);  
            createConsumerSportsPriority1Events(eventChannel, rootPoa);  
              
            orb.run();            
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
      
    public static void createConsumerAllEvents(EventChannel eventChannel, POA rootPoa)  
    throws UserException {  
        StructuredPushConsumerImpl pushConsumerImpl = new StructuredPushConsumerImpl(  
                "CosumerAllEvents");  
        ConsumerAdmin consumerAdmin = eventChannel.default_consumer_admin();  
        StructuredProxyPushSupplier proxyPushSupplier =  
                StructuredProxyPushSupplierHelper.narrow(  
                        consumerAdmin.obtain_notification_push_supplier(  
                                ClientType.STRUCTURED_EVENT, new IntHolder()));  
          
        FilterFactory filterFactory = eventChannel.default_filter_factory();  
          
        //Within a filter, individual constraints are combined using OR semantics.  
        //When multiple filters apply, OR is applied to the combination of results of filters.  
        ConstraintExp constraint1 = new ConstraintExp(  
                new EventType[]{new EventType("NewsBC", "Sports")}, "TRUE");  
        ConstraintExp constraint2 = new ConstraintExp(  
                new EventType[]{new EventType("NewsBC", "Economy")}, "TRUE");  
        Filter filter1 = filterFactory.create_filter("EXTENDED_TCL");  
            filter1.add_constraints(new ConstraintExp[]{constraint1, constraint2});  
          
        ConstraintExp constraint3 = new ConstraintExp(  
                new EventType[]{new EventType("NewsBC", "Entertainment")}, "TRUE");  
        Filter filter2 = filterFactory.create_filter("EXTENDED_TCL");  
            filter2.add_constraints(new ConstraintExp[]{constraint3});  
          
            proxyPushSupplier.add_filter(filter1);  
            proxyPushSupplier.add_filter(filter2);  
  
        proxyPushSupplier.connect_structured_push_consumer(  
                StructuredPushConsumerHelper.narrow(  
                        rootPoa.servant_to_reference(pushConsumerImpl)));  
    }  
      
    public static void createConsumerSportsPriority1Events(EventChannel eventChannel, POA rootPoa)  
    throws UserException {  
        StructuredPushConsumerImpl pushConsumerImpl = new StructuredPushConsumerImpl(  
                "CosumerSportsPrio1");  
        //When the results of filters associated with a proxy  
        //and those of filters associated with its admin object are combined,  
        //the actual Boolean operator depends on  
        //the value of the MyOperator attribute in the admin object.  
        ConsumerAdmin consumerAdmin = eventChannel.new_for_consumers(  
                InterFilterGroupOperator.AND_OP, new IntHolder());  
        System.out.println(consumerAdmin.MyOperator());  
        StructuredProxyPushSupplier proxyPushSupplier =  
                StructuredProxyPushSupplierHelper.narrow(  
                        consumerAdmin.obtain_notification_push_supplier(  
                                ClientType.STRUCTURED_EVENT, new IntHolder()));  
          
        FilterFactory filterFactory = eventChannel.default_filter_factory();  
          
        ConstraintExp constraint1 = new ConstraintExp(  
                new EventType[]{new EventType("NewsBC", "Sports")},  
                "$Priority<2");  
        Filter filter1 = filterFactory.create_filter("EXTENDED_TCL");  
            filter1.add_constraints(new ConstraintExp[]{constraint1});  
            consumerAdmin.add_filter(filter1);  
          
        ConstraintExp constraint2 = new ConstraintExp(  
                new EventType[]{new EventType("NewsBC", "Sports")},  
                "$.header.variable_header(Priority)>0");  
        Filter filter2 = filterFactory.create_filter("EXTENDED_TCL");  
            filter2.add_constraints(new ConstraintExp[]{constraint2});  
            proxyPushSupplier.add_filter(filter2);  
          
        proxyPushSupplier.connect_structured_push_consumer(  
                StructuredPushConsumerHelper.narrow(  
                        rootPoa.servant_to_reference(pushConsumerImpl)));  
          
        EventType[] eventTypes = proxyPushSupplier.obtain_offered_types(  
                ObtainInfoMode.ALL_NOW_UPDATES_OFF);  
        for (EventType eventType : eventTypes) {  
            System.out.println(eventType.domain_name + "." + eventType.type_name);  
        }  
    }  
  
}  